package cn.edu.zafu.demo.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hannesdorfmann.mosby.mvp.lce.MvpLceActivity;

import java.util.List;

import cn.edu.zafu.demo.R;
import cn.edu.zafu.demo.ui.adapter.NewsAdapter;
import cn.edu.zafu.demo.ui.bean.News;
import cn.edu.zafu.demo.ui.presenter.NewsPresenter;
import cn.edu.zafu.demo.ui.view.NewsView;

public class NewsActivity extends MvpLceActivity<SwipeRefreshLayout, List<News>, NewsView, NewsPresenter> implements NewsView, SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView recyclerView;
    private NewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        adapter = new NewsAdapter();
        contentView.setOnRefreshListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        loadData(false);
    }

    @NonNull
    @Override
    public NewsPresenter createPresenter() {
        return new NewsPresenter();
    }

    @Override
    public void showContent() {
        super.showContent();
        contentView.setRefreshing(false);
    }

    @Override
    public void showError(Throwable e, boolean pullToRefresh) {
        super.showError(e, pullToRefresh);
        contentView.setRefreshing(false);
    }

    @Override
    protected String getErrorMessage(Throwable e, boolean pullToRefresh) {
        return "发生了错误";
    }

    @Override
    public void setData(List<News> data) {
        adapter.setNews(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadData(boolean pullToRefresh) {
        presenter.loadNews(pullToRefresh);
    }

    @Override
    public void onRefresh() {
        contentView.setRefreshing(true);
        loadData(true);
    }
}
